cd "G:\LAD_All_years\LAD_Allyear_v6\data_donnees\stata"

forval j=1985/2019{	
	* use TIRC that is CRA income definition and what connolly uses in JOLE 
	use FIN* FAMWGT WGT* LIN* TIRC* AGE_* T4E__I* EI__* INDFL* TNKID* SXCO*  using "lad_dal_`j'_f1_v1.dta", clear

	* first we have to create a new weight and *5 (confirmed! and confirmed pweight)
	gen newweight=WGT2_I*5 
	
	* drop if ind income <0 
	* drop if partner income <0 
	*don't want child tax filers
			drop if INDFLI`j'==5| INDFLI`j'==6

    drop if TIRC_I`j'<0
	
	set seed 123 
	
	
	forval i=1/1000{
		
		bsample
		
		preserve 
			* TIRC: 
		* individual income
		pctile pctind_`i'age30=TIRC_I`j' if AGE__I`j'==30 [pweight=newweight], nq(100)
		pctile pctind_`i'age45=TIRC_I`j' if AGE__I`j'==45 [pweight=newweight], nq(100)

		keep pctind* 
		drop if _n>100
		
		gen year=`j'
			gen bsamplenum=`i'
		gen pct=_n
		save "H:\Zheng_10223\Joint\LAD\bootstrap\ind\pctind_sample`i'_year`j'.dta", replace 

		restore 
		
		********************** 
		preserve 
		* parents 
			* keep only those with kids and drop negative household income
		drop if TIRC_F`j'<0

	drop if TNKIDI`j'<1
	
	
	* ages: keep if the age group is equal to the value `j'
		

		
		
		gen mainageparent=AGE__I`j' if  INDFLI`j'==1|INDFLI`j'==2
		replace mainageparent=AGE__P`j' if   INDFLI`j'==3|INDFLI`j'==4
		
		* if lone parent:  
		replace mainageparent=AGE__I`j' if INDFLI`j'==7
		
		
		* get one family 
		* drop remaining duplicates
		bysort FIN__I`j': gen countfam=_n
		drop if countfam>1
		drop countfam
		
			* TIRC: 
		* individual income
		pctile pctparentage30=TIRC_P`j' if mainageparent==30 [pweight=newweight], nq(100)
		pctile pctparentage45=TIRC_P`j' if mainageparent==45 [pweight=newweight], nq(100)

		keep pctparent* 
		drop if _n>100
		
		gen year=`j'
		gen bsamplenum=`i'
		gen pct=_n
		save "H:\Zheng_10223\Joint\LAD\bootstrap\parents\pctparent_sample`i'_year`j'.dta", replace 

		restore 
		
		
		}
		
	}	 
		 	
	